library t3_map_widget;

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:t3_data_service/data_provider/map_sctx/t3_map_sctx_provider.dart';
import 'package:t3_map/t3_map_plugin/map/core/t3_location.dart';
import 't3_map_controller.dart';

const VIEW_TYPE = 'com.t3.map.flutter.mapview';

class T3MapCameraPosition {
  final T3LocationCoordinate2D centerCoordinate;

  const T3MapCameraPosition({required this.centerCoordinate});

  Map<String, dynamic> toJson() {
    return {
      'centerCoordinate': centerCoordinate.toJson()
    };
  }
}

class T3MapWidget extends StatefulWidget {

  /// 地图类型
  final T3MapType mapType;
  /// 用于与地图交互的控制器
  final T3MapController controller;

  /// 初始化时的地图中心点
  final T3MapCameraPosition? initialCameraPosition;

  ///需要应用到地图上的手势集合
  final Set<Factory<OneSequenceGestureRecognizer>> gestureRecognizers;

  const T3MapWidget({
    super.key,
    required this.mapType,
    required this.controller,
    this.initialCameraPosition,
    this.gestureRecognizers = const <Factory<OneSequenceGestureRecognizer>>{},
    });  
  
  @override
  State<StatefulWidget> createState() => _T3MapState();
}

class _T3MapState extends State<T3MapWidget> {

  @override
  Widget build(BuildContext context) {
    final Map<String, dynamic> creationParams = {
      'mapType': widget.mapType.value,
      'mapId': widget.controller.mapId
      // 'privacyStatement': widget.privacyStatement?.toMap(),
      // 'apiKey': widget.apiKey?.toMap(),
      // 'initialCameraPosition': widget.initialCameraPosition.toMap(),
      // 'options': _mapOptions.toMap(),
    };

    if (widget.initialCameraPosition != null) {
      creationParams['initialCameraPosition'] = (widget.initialCameraPosition!).toJson();
    }
    return _buildView(creationParams, widget.gestureRecognizers,_onPlatformViewCreated);
  }

  @override
  void dispose() async {
    super.dispose();
    print('dispose MapWidget with mapId:');
  }

  @override
  void reassemble() {
    super.reassemble();
    print('reassemble MapWidget');
  }

  @override
  void deactivate() async {
    super.deactivate();
    print('deactivate AMapWidget}');
  }

  Widget _buildView(
      Map<String, dynamic> creationParams,
      Set<Factory<OneSequenceGestureRecognizer>> gestureRecognizers,
      void Function(int id) onPlatformViewCreated) {
    if (defaultTargetPlatform == TargetPlatform.android) {
      creationParams['debugMode'] = kDebugMode;
      return AndroidView(
        viewType: VIEW_TYPE,
        onPlatformViewCreated: onPlatformViewCreated,
        gestureRecognizers: gestureRecognizers,
        creationParams: creationParams,
        creationParamsCodec: const StandardMessageCodec(),
      );
    } else if (defaultTargetPlatform == TargetPlatform.iOS) {
      return UiKitView(
        viewType: VIEW_TYPE,
        onPlatformViewCreated: onPlatformViewCreated,
        gestureRecognizers: gestureRecognizers,
        creationParams: creationParams,
        creationParamsCodec: const StandardMessageCodec(),
      );
    }
    return Text('当前平台:$defaultTargetPlatform, 不支持使用高德地图插件');
  }

  /// 平台view创建完成回调
  Future<void> _onPlatformViewCreated(int mapId) async {
    //TODO: 补充日志
  }
}